package com.unohacha.qqj.fzdr.personal.presenter;

import com.unohacha.qqj.fzdr.common.utils.TemporaryDataUtil;
import com.unohacha.qqj.fzdr.common.bean.HttpResult;
import com.unohacha.qqj.fzdr.common.http.RetrofitManager;
import com.unohacha.qqj.fzdr.personal.bean.UserInfoBean;
import com.unohacha.qqj.fzdr.personal.view.IAuthenticationView;
import com.unohacha.qqj.fzdr.util.Prefer;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AuthenticationPresenter implements IAuthenticationPresenter {

    private IAuthenticationView iAuthenticationView;

    public AuthenticationPresenter(IAuthenticationView iAuthenticationView) {
        this.iAuthenticationView = iAuthenticationView;
    }

    @Override
    public void getAuthenticationState() {
        // 请求认证 // 返回结果
      //  if (TemporaryDataUtil.getUserInfoBean() != null) {
        if (false) {
            iAuthenticationView.showAuthenticationState(
                    TemporaryDataUtil.getUserInfoBean().getType_ps(),
                    TemporaryDataUtil.getUserInfoBean().getType_ep());
        } else {
            Call<HttpResult<UserInfoBean>> call = RetrofitManager.getInstance().getUserInfo(Prefer.getInstance().getToken());
            call.enqueue(new Callback<HttpResult<UserInfoBean>>() {
                @Override
                public void onResponse(Call<HttpResult<UserInfoBean>> call, Response<HttpResult<UserInfoBean>> response) {
                    UserInfoBean userInfoBean = response.body().getData();
                    if (userInfoBean != null) {
                        iAuthenticationView.showAuthenticationState(userInfoBean.getType_ps(), userInfoBean.getType_ep());
                    }
                }

                @Override
                public void onFailure(Call<HttpResult<UserInfoBean>> call, Throwable t) {
                    iAuthenticationView.showAuthenticationState("", "");
                }
            });
        }
    }
}
